<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/21
 * Time: 18:32
 */

namespace app\controller\index\user;


use app\common\lib\Common;
use app\controller\CoreController;
use app\exception\IndexException;
use app\logic\index\user\UserLogic;
use app\logic\index\weixin\WeixinLogic;
use support\Request;

class LoginController extends CoreController
{
    public function guestLogin (Request $request)
    {
        $param = [];
        $result = UserLogic::getInstance()->guestLogin($param);
        return $this->endSuccess($result);
    }

    public function weixinLogin(Request $request)
    {
        $param = [];
        $param['code'] = $request->input('code',null);
        if(empty($param['code'])){
            throw new IndexException(4020,'code不能为空');
        }
        $data = WeixinLogic::getInstance()->getWeixinLogin($param);
        return $this->endSuccess($data);
    }

    /**
     * 一键登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws IndexException
     */
    public function oneKeyLogin(Request $request)
    {
        $param = [];
        $param['yi_dun_token'] = $request->input('yi_dun_token',null);
        $param['yi_dun_access_token'] = $request->input('yi_dun_access_token',null);

        if(empty($param['yi_dun_token'])){
            throw new IndexException(4020,'易盾token不能为空');
        }
        if(empty($param['yi_dun_access_token'])){
            throw new IndexException(4020,'易盾access_token不能为空');
        }

        $data = UserLogic::getInstance()->oneKeyLogin($param);

        return $this->endSuccess($data);
    }

    /**
     * 手机号登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws IndexException
     */
    public function loginByAuthcode(Request $request)
    {
        $param = [];
        $param['mobile'] = $request->input('mobile',null);
        $param['authcode'] = $request->input('authcode',null);
        if(empty($param['mobile'])){
            throw new IndexException(4020,'手机号不能为空');
        }
        if(empty($param['authcode']) && empty($param['password'])){
            throw new IndexException(4020,'验证码不能为空');
        }

        $data = UserLogic::getInstance()->loginByAuthcode($param);

        return $this->endSuccess($data);
    }

    /**
     * 手机号注册
     * @param Request $request
     * @return mixed
     * @throws IndexException
     */
    public function registerToScene(Request $request)
    {
        $param = [];
        $param['mobile'] = $request->input('mobile',null);
        $param['scene'] = $request->input('scene',null);

        if(empty($param['mobile'])){
            throw new IndexException(4020,'手机号不能为空');
        }
        if(empty($param['scene']) && Common::isAuthInviteCode()==true){
            //throw new IndexException(4020,'邀请码不能为空');
        }

        $data = UserLogic::getInstance()->registerToScene($param);

        return $this->endSuccess($data);
    }

    /**
     * 手机号注册-h5
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws IndexException
     */
    public function registerByMobile(Request $request)
    {
        $param = [];
        $param['mobile'] = $request->input('mobile',null);
        $param['authcode'] = $request->input('authcode',null);
        $param['scene'] = $request->input('scene',null);
        if(empty($param['mobile'])){
            throw new IndexException(2008,'手机号不能为空');
        }
        if(empty($param['authcode'])){
            throw new IndexException(2008,'验证码不能为空');
        }
        if(empty($param['scene'])){
            throw new IndexException(2008,'邀请码不能为空');
        }

        $data = UserLogic::getInstance()->registerByMobile($param);

        return $this->endSuccess($data);
    }
}